package org.maxkey.portal.core.authentication;

import org.apereo.cas.authentication.Credential;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Credential for authenticating with a username and password.
 *
 * @author Scott Battaglia
 * @author Marvin S. Addison
 * @since 3.0.0
 */
@ToString(exclude = "password")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
//@ApiModel("凭据对象")
public class UsernamePasswordCredential implements Credential {
    /**
     * Authentication attribute name for password.
     **/
    public static final String AUTHENTICATION_ATTRIBUTE_PASSWORD = "credential";

    private static final long serialVersionUID = -700605081472810939L;

   // @ApiModelProperty(value="用户名",required=true)
    private  String username;
    //@ApiModelProperty(value="密码",required=true)
    private  String password;

    private boolean sso=false;

    private String source;

    private Map<String, Object> customFields = new LinkedHashMap<>();

    public UsernamePasswordCredential(final String username, final String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    public String getId() {
        return this.username;
    }


    /**
     * 验证
     * @return
     */
    public boolean validate() {
        if(this.sso){
            if (StringUtils.isEmpty(this.username)) {
                return false;
            }
        }else {
            if (StringUtils.isEmpty(this.username)) {
                return false;
            }
            if (StringUtils.isEmpty(this.password)) {
                return false;
            }
        }
        return  true;
    }
}
